package com.augurit.gzcgwsp.state.service.impl;

import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.augurit.gzcgwsp.common.util.CommonUtils;
import com.augurit.gzcgwsp.commonex.util.lang.DateUtilsEX;
import com.augurit.gzcgwsp.excelimportexport.util.FileUtil;
import com.augurit.gzcgwsp.excelimportexport.util.GetFileName;
import com.augurit.gzcgwsp.state.dao.YwXzXkSpStateDao;
import com.augurit.gzcgwsp.state.entity.UserStateInfo;
import com.augurit.gzcgwsp.state.service.ISummaryService;
import com.augurit.gzcgwsp.state.service.IYwSummaryStateService;
import com.augurit.gzcgwsp.state.util.ItextWordUtil;
import com.augurit.gzcgwsp.state.web.form.SummaryStateForm;
import com.augurit.gzcgwsp.ywcmn.entity.BaseDistrict;
import com.augurit.wf.bi.service.IWfBusTemplateService;
import com.augurit.wf.bi.service.IWfBusTemplateTypeService;
import com.augurit.wf.bi.web.form.WfBusTemplateForm;
import com.augurit.wf.bi.web.form.WfBusTemplateTypeForm;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;

/**
 * 行政许可审批查询统计service实现类
 * 
 * @author 张军
 * 
 */
@Service
@Transactional
public class SummaryServiceImpl implements ISummaryService {
	private Logger log = Logger.getLogger(SummaryServiceImpl.class);
	@Autowired
	private IWfBusTemplateService wfBusTemplateService;
	 @Autowired
	private IWfBusTemplateTypeService wfBusTemplateTypeService;
	@Autowired
	private YwXzXkSpStateDao ywXzXkSpStateDao;
	@Autowired
	private IYwSummaryStateService ywSummaryStateService;
	
	/**
	 * 构建sql
	 * @param map
	 * @return 
	 * @date 2012-4-19
	 */
	private String getTotalSql(Map map){
		String depts=this.buildSqlBySetBus(map);
		StringBuffer sb=new StringBuffer();
		sb.append("select count(1) as countnum,t.template_name,");
		sb.append("t.template_code,doc.file_code,t.timetype from ( \r");
		sb.append("ins.master_entity_key,case when proc.end_<=proc.process_duedate then 'ontime' \r");
		sb.append(" when proc.end_>proc.process_duedate then 'overtime'  \r");
		sb.append("else 'notdata' end as timetype from \r");
		sb.append("JBPM4_HIST_PROCINST proc,WF_BUS_INSTANCE ins,WF_BUS_TEMPLATE temp  \r");
		sb.append(" where proc.id_=ins.proc_inst_id \r");
		sb.append(" and ins.template_id=temp.id    and proc.state_='ended' \r");
		sb.append(" and proc.process_duedate is not null )\r");
		sb.append(" t left join YW_PRINT_DOC doc on doc.yw_id=t.master_entity_key and  \r");
		sb.append("doc.template_code=t.template_code  and doc.file_code in ('byxkjds','qubyxkjds')   \r");
		sb.append(" group by doc.file_code,t.template_code, t.template_name,t.timetype");
		return sb.toString();
	}
	/**
	 * 构建超时sql
	 * @param map
	 * @return 
	 * @date 2012-4-19
	 */
	private String getOverTimeSql(Map map,String type){
		String []times=this.getTimeByParam(map);
		String depts=this.buildSqlBySetBus(map);
	    StringBuffer sb=new StringBuffer();
		sb.append("select  count(1) as countnum,temp.template_name, temp.template_code, \r");
		sb.append("case when proc.end_<=proc.process_duedate then 'ontime'  \r");
		sb.append(" when proc.end_>proc.process_duedate then 'overtime'   else 'notdata' end as timetype from \r");
		sb.append(" JBPM4_HIST_PROCINST proc,WF_BUS_INSTANCE ins,WF_BUS_TEMPLATE temp   \r");
		sb.append(" ,wf_bus_template_type temptype where proc.id_=ins.proc_inst_id  \r");
		sb.append(" and ins.template_id=temp.id    and proc.state_='ended'    \r");
		sb.append("  and temptype.id=temp.template_type_id  \r");
		sb.append("and proc.process_duedate is not null \r");
		sb.append(" and ins.bus_date2>=to_date('"+times[0]+"','yyyy-MM-dd')\r");
		sb.append(" and ins.bus_date2<=to_date('"+times[1]+"','yyyy-MM-dd')\r");
		if(StringUtils.isNotBlank(type)){
			sb.append(" and temptype.template_type_name ='"+type+"'");
		}else{
			sb.append(" and temptype.template_type_name in("+depts+")");
		}
		
		sb.append(" group by case when proc.end_<=proc.process_duedate then 'ontime'  \r");
		sb.append("  when proc.end_>proc.process_duedate then 'overtime'   else 'notdata' end \r");
		sb.append(" , temp.template_code, temp.template_name\r");
	 return sb.toString();
	}	
	/**
	 * 构建sql,凡是有准予许可决定书的案件就为同意
	 * @param map
	 * @return 
	 * @date 2012-4-19
	 */
	private String getAgreeSql(Map map,String typename){
		String []times=this.getTimeByParam(map);
		String depts="";
		if(StringUtils.isBlank(typename)){
			depts=this.buildSqlBySetBus(map);
		}else{
			depts=typename;
		}
		StringBuffer sb=new StringBuffer();
		sb.append(" select count(1) as countnum,t.template_name,t.template_code,doc.file_code,t.template_type_name from (  \r");
		sb.append("select   temp.template_code,ins.master_entity_key,temp.template_name,temptype.template_type_name from   \r");
		sb.append(" JBPM4_HIST_PROCINST proc,WF_BUS_INSTANCE ins, \r");
		sb.append("  WF_BUS_TEMPLATE temp,wf_bus_template_type temptype  where proc.id_=ins.proc_inst_id     \r");
		sb.append("  and temptype.id=temp.template_type_id  \r");
		sb.append(" and ins.template_id=temp.id    and proc.state_='ended'  \r");
		sb.append(" and ins.bus_date2>=to_date('"+times[0]+"','yyyy-MM-dd')\r");
		sb.append(" and ins.bus_date2<=to_date('"+times[1]+"','yyyy-MM-dd')\r");
		sb.append(" and temptype.template_type_name in("+depts+")");
		sb.append(" ) t left join YW_PRINT_DOC doc on doc.yw_id=t.master_entity_key and   \r");
		sb.append("  doc.template_code=t.template_code  and doc.file_code in('zyxzxkjds','byxkjds','quzyxzxkjds','qubyxkjds','qubysltzs','bysltzs')   \r");
		sb.append("  group by doc.file_code,t.template_name,t.template_code,t.template_type_name   \r");
		return sb.toString();
	}

	/**
	 * 有相同数据的记录包括有补正通知书和准予许可通知书
	 * @return
	 */
	private int getSameData(String tempCode,boolean flag,Map map){
		//设置时间
		String[] times=this.getTimeByParam(map);

		String sql="select count(1) as countnum from yw_print_doc t where t.yw_id in ( \r";
				sql+="select ts.yw_id from yw_print_doc ts \r";
				sql+="where ts.creat_date>=to_date('"+times[0]+"','yyyy-MM-dd') and ts.creat_date<=to_date('"+times[1]+"','yyyy-MM-dd') and ts.file_code in('zyxzxkjds','byxkjds','quzyxzxkjds','qubyxkjds') and ts.yw_id=t.yw_id \r";
				if(flag){
					sql+=") and (t.file_code='bzcltzs' or t.file_code='qubzcltzs') and t.template_code='"+tempCode+"' \r";
				}else{
					sql+=") and (t.file_code='bzcltzs' or t.file_code='qubzcltzs') \r";
				}
				
		List list=this.ywXzXkSpStateDao.queryForList(sql);
		if(list.size()>0){
			 String num=(String) ((Map)list.get(0)).get("countnum");
			 return Integer.valueOf(num);
		}else
			return 0;
	}
	
	
	/**
	 * 获取用户设置的处室,用于查询
	 * @return 
	 * @date 2012-4-22
	 */
	private String buildSqlBySetBus(Map map ){
		UserStateInfo info=(UserStateInfo) map.get("UserStateInfo"); 
		String typename="'燃气','景观','设施','市容','环卫','余泥'";
		String result="";
		if(info!=null){
			String depts=info.getStateBusiness();
			String[] strs=depts.split(",");
			for(String str:strs){
				if(str.indexOf("处")>0){
					str=str.substring(0,str.length()-1);
					result+="'"+str+"'"+",";
				}else{
					result+="'"+str+"'"+",";
				}
			}
			if(result.length()>0){
				result=result.substring(0,result.length()-1);
			}
			return result;
		}else{
			return typename;
		}
	}
	
	
	
	/**
	 * 获取根据业务编号获取完成时间的数据
	 * @param map
	 * @return 
	 * @date 2012-4-22
	 */
	private String getTimesData(List data,String tempcode,boolean isontime){
		String count="0";
		for(int i=0;i<data.size();i++){
			Map map=(Map) data.get(i);
			String timetype=(String) map.get("timetype");
			String temlatecode=(String) map.get("template_code");
			String countnum=(String) map.get("countnum")==null?"0":(String) map.get("countnum");
			if(isontime){//按时
				if("ontime".equals(timetype)){
					if(temlatecode.equals(tempcode)){
						count=countnum;
						break;
					}
				}
			}else{//超时
				if("overtime".equals(timetype)){
					if(temlatecode.equals(tempcode)){
						count=countnum;
						break;
					}
				}
			}
		}
		return count;
	}
	/**
	 * 获取根据业务编号获取同意的数据
	 * @param map
	 * @return 
	 * @date 2012-4-22
	 */
	private String getAgreeData(List data,String tempcode,String filecode,boolean isAgree){
		String count="0";
    	
		for(int i=0;i<data.size();i++){
			Map map=(Map) data.get(i);
			String file_code=(String) map.get("file_code");
			String temlatecode=(String) map.get("template_code");
			WfBusTemplateForm template=wfBusTemplateService.getByTemplateCode(temlatecode);
	    	Long tempTypeId=template.getTemplateTypeId();
	    	WfBusTemplateTypeForm type=wfBusTemplateTypeService.get(tempTypeId);
			
			String countnum=(String) map.get("countnum")==null?"0":(String) map.get("countnum");	
			if(!"hwggshsqb".equals(temlatecode) ||"余泥".equals(type.getTemplateTypeName())
					||"环卫".equals(type.getTemplateTypeName())){
				//景观环卫余泥没有决定书设置为同意
				if(temlatecode.equals(tempcode)){
					if(StringUtils.isNotBlank(filecode)){
						if(StringUtils.isNotBlank(file_code)){
							if(filecode.equals(file_code)){
								count=countnum;
								break;
							}
						}
					}else{
						if(StringUtils.isBlank(file_code)){
						//其他
						count=countnum;
						break;
						}
					}
			
					
				}
			}else{
				if(temlatecode.equals(tempcode)){
					if(isAgree){
						if(StringUtils.isBlank(file_code)){
							count=countnum;
							break;
						}
					}else{
						if(StringUtils.isNotBlank(file_code)){
							if(filecode.equals(file_code)){
								count=countnum;
								break;
							}
						}
						
					}
			}
				
		  }
	   }
		return count;
	}
	
	
	/**
	 * 查询统计数据
	 */
	public SummaryStateForm searchInfo(Map map) {
		SummaryStateForm form=new SummaryStateForm();
		String agreeSql=this.getAgreeSql(map,null);
		String overTimeSql=this.getOverTimeSql(map,null);
		//查询同意或者不同意
		List agreeData=this.ywXzXkSpStateDao.queryForList(agreeSql);
		//查询是否超时
		List overTimeData=this.ywXzXkSpStateDao.queryForList(overTimeSql);
		int agreecount=0;
		int notagreecount=0;
		int ontimecount=0;
		int overtimecount=0;
		int gzcount=0;
		int othercount=0;
		for(int i=0;i<agreeData.size();i++){
			Map data=(Map) agreeData.get(i);
			String filecode=(String) data.get("file_code");
			String tempcode=(String) data.get("template_code");
			String countnum=(String) data.get("countnum")==null?"0":(String) data.get("countnum");
			if(StringUtils.isNotBlank(filecode)){
				if("zyxzxkjds".equals(filecode)){
					agreecount+=Integer.valueOf(countnum);
				}else if("byxkjds".equals(filecode)){
					notagreecount+=Integer.valueOf(countnum);
				}else if("bzcltzs".equals(filecode)){
					gzcount+=Integer.valueOf(countnum);
				}else if("bysltzs".equals(filecode)){
					othercount+=Integer.valueOf(countnum);
				}
			}else{
				WfBusTemplateForm template=wfBusTemplateService.getByTemplateCode(tempcode);
		    	Long tempTypeId=template.getTemplateTypeId();
		    	WfBusTemplateTypeForm type=wfBusTemplateTypeService.get(tempTypeId);	
				if("hwggshsqb".equals(tempcode) ||"余泥".equals(type.getTemplateTypeName())
						){
				   //如果有些业务没有文书就当成通过
					agreecount+=Integer.valueOf(countnum);
				}else if("环卫".equals(type.getTemplateTypeName())){
					agreecount+=Integer.valueOf(countnum);
				}else{
					othercount+=Integer.valueOf(countnum);
				}
			}
			form.setRqothercount(othercount);
			
			this.setAgreeDataByTempCode(form, data);
		}
		
		for(int i=0;i<overTimeData.size();i++){
			Map data=(Map) overTimeData.get(i);
			String timetype=(String) data.get("timetype");
			String countnum=(String) data.get("countnum")==null?"0":(String) data.get("countnum");
			if("overtime".equals(timetype)){
				overtimecount+=Integer.valueOf(countnum);
			}else if("ontime".equals(timetype)){
				ontimecount+=Integer.valueOf(countnum);
			}
			this.setOntimeDataByTempCode(form, data);
		}
		int samedatacount=this.getSameData("", false,map);
		if(gzcount>=samedatacount){
			gzcount=gzcount-samedatacount;
		}
		form.setAgreecount(agreecount);//目前景观处没有制文，所以把景观的所有审批看成通过
		form.setNotagreecount(notagreecount);
		form.setOvertimecount(overtimecount);
		form.setOntimecount(ontimecount);
		form.setGzCount(gzcount);
		//设置时间
		String[] times=this.getTimeByParam(map);
		form.setStarttime(times[0]);
		form.setEndtime(times[1]);
		return form;
		
	}

	/**
	 * 设置同意不同意,其他（不予受理加退案的总和）
	 * @param temlatecode
	 * @param fileCode 
	 * @date 2012-4-19
	 */
	private void setAgreeDataByTempCode(SummaryStateForm form,Map map){
		String filecode=(String) map.get("file_code");
		String temlatecode=(String) map.get("template_code");
		String countnum=(String) map.get("countnum")==null?"0":(String) map.get("countnum");
		if("rqjyxkz_pzqy".equals(temlatecode)){
			if("byxkjds".equals(filecode)||"qubyxkjds".equals(filecode)){//不同意
				form.setPzgasnumNoAgree(Integer.valueOf(countnum));
			}else if("zyxzxkjds".equals(filecode)||"quzyxzxkjds".equals(filecode)){
				form.setPzgasnumAgree(Integer.valueOf(countnum));
			}else if("bzcltzs".equals(filecode)||"qubzcltzs".equals(filecode)){
				//减去(准予不准予)和一次性告知都有的数量
				int sameData=this.getSameData(temlatecode,true,map);
				form.setPzgasnumGz(Integer.valueOf(countnum)-sameData);
			}else if("bysltzs".equals(filecode)||"qubysltzs".equals(filecode)){
				form.setPzgasothernum(form.getPzgasothernum()+Integer.valueOf(countnum));
			}else if(StringUtils.isBlank(filecode)){
				form.setPzgasothernum(form.getPzgasothernum()+Integer.valueOf(countnum));
			}
			
		}else if("rqjyxkz_gdqy".equals(temlatecode)){
			if("byxkjds".equals(filecode)){//不同意
				form.setGdgasnumNoAgree(Integer.valueOf(countnum));
			}else if("zyxzxkjds".equals(filecode)){
				form.setGdgasnumAgree(Integer.valueOf(countnum));
			}else if("bzcltzs".equals(filecode)){
				form.setGdgasnumGz(Integer.valueOf(countnum));
			}else if("bysltzs".equals(filecode)){
				form.setGdgasothernum(form.getGdgasothernum()+Integer.valueOf(countnum));
			}else if(StringUtils.isBlank(filecode)){
				form.setGdgasothernum(form.getGdgasothernum()+Integer.valueOf(countnum));
			}
		}else if("rqssgd".equals(temlatecode)){
			if("byxkjds".equals(filecode)){//不同意
				form.setSsgdnumNoAgree(Integer.valueOf(countnum));
			}else if("zyxzxkjds".equals(filecode)){
				form.setSsgdnumAgree(Integer.valueOf(countnum));
			}else if("bzcltzs".equals(filecode)){
				form.setSsgdnumGz(Integer.valueOf(countnum));
			}else if("bysltzs".equals(filecode)){
				form.setSsgdothernum(form.getSsgdothernum()+Integer.valueOf(countnum));
			}else if(StringUtils.isBlank(filecode)){
				form.setSsgdothernum(form.getSsgdothernum()+Integer.valueOf(countnum));
			}
		}else if("rqrsqjazwxqyzz".equals(temlatecode)){
			if("byxkjds".equals(filecode)){//不同意
				form.setQyzznumNoAgree(Integer.valueOf(countnum));
			}else if("zyxzxkjds".equals(filecode)){
				form.setQyzznumAgree(Integer.valueOf(countnum));
			}else if("bzcltzs".equals(filecode)){
				form.setQyzznumGz(Integer.valueOf(countnum));
			}else if("bysltzs".equals(filecode)){
				form.setQyzzothernum(form.getQyzzothernum()+Integer.valueOf(countnum));
			}else if(StringUtils.isBlank(filecode)){
				form.setQyzzothernum(form.getQyzzothernum()+Integer.valueOf(countnum));
			}
		}else if("hwggshsqb".equals(temlatecode)){//户外广告设置不同其他的统计
			if("byxkjds".equals(filecode)){//不同意
				form.setHwggnumNoAgree(Integer.valueOf(countnum));
			}else{//没有文书表示同意
				form.setHwggnumAgree(Integer.valueOf(countnum));
			}
			
		}else if("ccgbggcs".equals(temlatecode)){
			if("byxkjds".equals(filecode)){//不同意
				form.setWcnumNoAgree(Integer.valueOf(countnum));
			}else if("zyxzxkjds".equals(filecode)){
				form.setWcnumAgree(Integer.valueOf(countnum));
			}else if("bzcltzs".equals(filecode)){
				form.setWcnumGz(Integer.valueOf(countnum));
			}
		}else if("csdlwj".equals(temlatecode)){
			if("byxkjds".equals(filecode)){//不同意
				form.setDlwjnumNoAgree(Integer.valueOf(countnum));
			}else if("zyxzxkjds".equals(filecode)){
				form.setDlwjnumAgree(Integer.valueOf(countnum));
			}else if("bzcltzs".equals(filecode)){
				form.setDlwjnumGz(Integer.valueOf(countnum));
			}
		}else if("csdllszysp".equals(temlatecode)){
			if("byxkjds".equals(filecode)){//不同意
				form.setDlzynumNoAgree(Integer.valueOf(countnum));
			}else if("zyxzxkjds".equals(filecode)){
				form.setDlzynumAgree(Integer.valueOf(countnum));
			}else if("bzcltzs".equals(filecode)){
				form.setDlzynumGz(Integer.valueOf(countnum));
			}
		}else if("shljqssj".equals(temlatecode)){//陆域城市生活垃圾
			if("byxkjds".equals(filecode)){//不同意
				form.setLynumNoAgree(Integer.valueOf(countnum));
			}else if("zyxzxkjds".equals(filecode)||StringUtils.isBlank(filecode)){
				form.setLynumAgree(form.getLynumAgree()+Integer.valueOf(countnum));
			}else if("bzcltzs".equals(filecode)){
				form.setLynumGz(Integer.valueOf(countnum));
			}else{
				form.setLynumAgree(form.getLynumAgree()+Integer.valueOf(countnum));
			}
				
		}else if("shljys".equals(temlatecode)){
			if("byxkjds".equals(filecode)){//不同意
				form.setSynumNoAgree(Integer.valueOf(countnum));
			}else if("zyxzxkjds".equals(filecode)||StringUtils.isBlank(filecode)){
				form.setSynumAgree(form.getSynumAgree()+Integer.valueOf(countnum));
			}else if("bzcltzs".equals(filecode)){
				form.setSynumGz(Integer.valueOf(countnum));
			}else{
				form.setSynumAgree(form.getSynumAgree()+Integer.valueOf(countnum));
			}
		}else if("shljcz".equals(temlatecode)){
			if("byxkjds".equals(filecode)){//不同意
				form.setCznumNoAgree(Integer.valueOf(countnum));
			}else if("zyxzxkjds".equals(filecode)||StringUtils.isBlank(filecode)){
				form.setCznumAgree(Integer.valueOf(countnum));
			}else if("bzcltzs".equals(filecode)){
				form.setCznumGz(Integer.valueOf(countnum));
			}
		}else if("shljsyba".equals(temlatecode)){
			if("byxkjds".equals(filecode)){//不同意
				form.setSybanumNoAgree(Integer.valueOf(countnum));
			}else if("zyxzxkjds".equals(filecode)||StringUtils.isBlank(filecode)){
				form.setSybanumAgree(Integer.valueOf(countnum)+form.getSybanumAgree());
			}else if("bzcltzs".equals(filecode)){
				form.setSybanumGz(Integer.valueOf(countnum));
			}else{
				form.setSybanumAgree(form.getSybanumAgree()+Integer.valueOf(countnum));
			}
		}else if("shljqssjba".equals(temlatecode)){
			if("byxkjds".equals(filecode)){//不同意
				form.setLybanumNoAgree(Integer.valueOf(countnum));
			}else if("zyxzxkjds".equals(filecode)||StringUtils.isBlank(filecode)){
				form.setLybanumAgree(Integer.valueOf(countnum)+form.getLybanumAgree());
			}else if("bzcltzs".equals(filecode)){
				form.setLybanumGz(Integer.valueOf(countnum));
			}else{
				form.setLybanumAgree(Integer.valueOf(countnum)+form.getLybanumAgree());
			}
		}else if("jzljczz_xzfz".equals(temlatecode)){//建筑废弃物处置证(运输)==>新增发证
			if("byxkjds".equals(filecode)){//不同意
				form.setTransportXzfznumNoAgree(Integer.valueOf(countnum));
			}else if("zyxzxkjds".equals(filecode)||StringUtils.isBlank(filecode)){
				form.setTransportXzfznumAgree(Integer.valueOf(countnum));
			}else if("bzcltzs".equals(filecode)){
				form.setTransportXzfznumGz(Integer.valueOf(countnum));
			}
		}else if("jzljczz_nsxz".equals(temlatecode)){
			if("byxkjds".equals(filecode)){//不同意
				form.setTransportNsxznumNoAgree(Integer.valueOf(countnum));
			}else if("zyxzxkjds".equals(filecode)||StringUtils.isBlank(filecode)){
				form.setTransportNsxznumAgree(Integer.valueOf(countnum));
			}else if("bzcltzs".equals(filecode)){
				form.setTransportNsxznumGz(Integer.valueOf(countnum));
			}
		}else if("jzljczz_bghz".equals(temlatecode)){
			if("byxkjds".equals(filecode)){//不同意
				form.setTransportBghznumNoAgree(Integer.valueOf(countnum));
			}else if("zyxzxkjds".equals(filecode)||StringUtils.isBlank(filecode)){
				form.setTransportBghznumAgree(Integer.valueOf(countnum));
			}else if("bzcltzs".equals(filecode)){
				form.setTransportBghznumGz(Integer.valueOf(countnum));
			}
		}else if("jzljczz_ysbb".equals(temlatecode)){
			if("byxkjds".equals(filecode)){//不同意
				form.setTransportYsbbnumNoAgree(Integer.valueOf(countnum));
			}else if("zyxzxkjds".equals(filecode)||StringUtils.isBlank(filecode)){
				form.setTransportYsbbnumAgree(Integer.valueOf(countnum));
			}else if("bzcltzs".equals(filecode)){
				form.setTransportYsbbnumGz(Integer.valueOf(countnum));
			}
		}else if("jzljczz_shhz".equals(temlatecode)){
			if("byxkjds".equals(filecode)){//不同意
				form.setTransportShhznumNoAgree(Integer.valueOf(countnum));
			}else if("zyxzxkjds".equals(filecode)||StringUtils.isBlank(filecode)){
				form.setTransportShhznumAgree(Integer.valueOf(countnum));
			}else if("bzcltzs".equals(filecode)){
				form.setTransportShhznumGz(Integer.valueOf(countnum));
			}
		}else if("jzfqwczzxn_xz".equals(temlatecode)){//建筑废弃物处置证(消纳)==>新增
			if("byxkjds".equals(filecode)){//不同意
				form.setBuildwasteXnnumNoAgree(Integer.valueOf(countnum));
			}else if("zyxzxkjds".equals(filecode)||StringUtils.isBlank(filecode)){
				form.setBuildwasteXnnumAgree(Integer.valueOf(countnum));
			}else if("bzcltzs".equals(filecode)){
				form.setBuildwasteXnnumGz(Integer.valueOf(countnum));
			}
		}else if("jzfqwczzpf_xz".equals(temlatecode)){//建筑废弃物处置证(排放)==>新增
			if("byxkjds".equals(filecode)){//不同意
				form.setBuildwasteLetnumNoAgree(Integer.valueOf(countnum));
			}else if("zyxzxkjds".equals(filecode)||StringUtils.isBlank(filecode)){
				form.setBuildwasteLetnumAgree(Integer.valueOf(countnum));
			}else if("bzcltzs".equals(filecode)){
				form.setBuildwasteLetnumGz(Integer.valueOf(countnum));
			}
		}
	}
	
	/**
	 * 超时或者不超时的数据
	 * @param form
	 * @param map 
	 * @date 2012-4-19
	 */
	private void setOntimeDataByTempCode(SummaryStateForm form,Map map){
		String timetype=(String) map.get("timetype");
		String temlatecode=(String) map.get("template_code");
		String countnum=(String) map.get("countnum")==null?"0":(String) map.get("countnum");
		if("rqjyxkz_pzqy".equals(temlatecode)){
			if("overtime".equals(timetype)){
				form.setPzgasnumOvertime(Integer.valueOf(countnum));
			}else if("ontime".equals(timetype)){
				form.setPzgasnumOntime(Integer.valueOf(countnum));
			}
			
		}else if("rqjyxkz_gdqy".equals(temlatecode)){
			if("overtime".equals(timetype)){
				form.setGdgasnumOvertime(Integer.valueOf(countnum));
			}else if("ontime".equals(timetype)){
				form.setGdgasnumOntime(Integer.valueOf(countnum));
			}
		}else if("rqssgd".equals(temlatecode)){
			if("overtime".equals(timetype)){
				form.setSsgdnumOvertime(Integer.valueOf(countnum));
			}else if("ontime".equals(timetype)){
				form.setSsgdnumOntime(Integer.valueOf(countnum));
			}
		}else if("rqrsqjazwxqyzz".equals(temlatecode)){
			if("overtime".equals(timetype)){
				form.setQyzznumOvertime(Integer.valueOf(countnum));
			}else if("ontime".equals(timetype)){
				form.setQyzznumOntime(Integer.valueOf(countnum));
			}
		}else if("hwggshsqb".equals(temlatecode)){
			if("overtime".equals(timetype)){
				form.setHwggnumOvertime(Integer.valueOf(countnum));
			}else if("ontime".equals(timetype)){
				form.setHwggnumOntime(Integer.valueOf(countnum));
			}
		}else if("ccgbggcs".equals(temlatecode)){
			if("overtime".equals(timetype)){
				form.setWcnumOvertime(Integer.valueOf(countnum));
			}else if("ontime".equals(timetype)){
				form.setWcnumOntime(Integer.valueOf(countnum));
			}
		}else if("csdlwj".equals(temlatecode)){
			if("overtime".equals(timetype)){
				form.setDlwjnumOvertime(Integer.valueOf(countnum));
			}else if("ontime".equals(timetype)){
				form.setDlwjnumOntime(Integer.valueOf(countnum));
			}
		}else if("csdllszysp".equals(temlatecode)){
			if("overtime".equals(timetype)){
				form.setDlzynumOvertime(Integer.valueOf(countnum));
			}else if("ontime".equals(timetype)){
				form.setDlzynumOntime(Integer.valueOf(countnum));
			}
		}else if("shljqssj".equals(temlatecode)){//陆域城市生活垃圾
			if("overtime".equals(timetype)){
				form.setLynumOvertime(Integer.valueOf(countnum));
			}else if("ontime".equals(timetype)){
				form.setLynumOntime(Integer.valueOf(countnum));
			}
		}else if("shljys".equals(temlatecode)){
			if("overtime".equals(timetype)){
				form.setSynumOvertime(Integer.valueOf(countnum));
			}else if("ontime".equals(timetype)){
				form.setSynumOntime(Integer.valueOf(countnum));
			}
		}else if("shljcz".equals(temlatecode)){
			if("overtime".equals(timetype)){
				form.setCznumOvertime(Integer.valueOf(countnum));
			}else if("ontime".equals(timetype)){
				form.setCznumOntime(Integer.valueOf(countnum));
			}
		}else if("shljsyba".equals(temlatecode)){//陆域项目登记
			if("overtime".equals(timetype)){
				form.setSybanumOvertime(Integer.valueOf(countnum));
			}else if("ontime".equals(timetype)){
				form.setSybanumOntime(Integer.valueOf(countnum));
			}
		}else if("shljqssjba".equals(temlatecode)){
			if("overtime".equals(timetype)){
				form.setLybanumOvertime(Integer.valueOf(countnum));
			}else if("ontime".equals(timetype)){
				form.setLybanumOntime(Integer.valueOf(countnum));
			}
		}else if("jzljczz_xzfz".equals(temlatecode)){//建筑废弃物处置证(运输)==>新增发证
			if("overtime".equals(timetype)){
				form.setTransportXzfznumOvertime(Integer.valueOf(countnum));
			}else if("ontime".equals(timetype)){
				form.setTransportXzfznumOntime(Integer.valueOf(countnum));
			}
		}else if("jzljczz_nsxz".equals(temlatecode)){
			if("overtime".equals(timetype)){
				form.setTransportNsxznumOvertime(Integer.valueOf(countnum));
			}else if("ontime".equals(timetype)){
				form.setTransportNsxznumOntime(Integer.valueOf(countnum));
			}
		}else if("jzljczz_bghz".equals(temlatecode)){
			if("overtime".equals(timetype)){
				form.setTransportBghznumOvertime(Integer.valueOf(countnum));
			}else if("ontime".equals(timetype)){
				form.setTransportBghznumOntime(Integer.valueOf(countnum));
			}
		}else if("jzljczz_ysbb".equals(temlatecode)){
			if("overtime".equals(timetype)){
				form.setTransportYsbbnumOvertime(Integer.valueOf(countnum));
			}else if("ontime".equals(timetype)){
				form.setTransportYsbbnumOntime(Integer.valueOf(countnum));
			}
		}else if("jzljczz_shhz".equals(temlatecode)){
			if("overtime".equals(timetype)){
				form.setTransportShhznumOvertime(Integer.valueOf(countnum));
			}else if("ontime".equals(timetype)){
				form.setTransportShhznumOntime(Integer.valueOf(countnum));
			}
		}else if("jzfqwczzxn_xz".equals(temlatecode)){
			if("overtime".equals(timetype)){
				form.setBuildwasteXnnumOvertime(Integer.valueOf(countnum));
			}else if("ontime".equals(timetype)){
				form.setBuildwasteXnnumOntime(Integer.valueOf(countnum));
			}
		}else if("jzfqwczzpf_xz".equals(temlatecode)){
			if("overtime".equals(timetype)){
				form.setBuildwasteLetnumOvertime(Integer.valueOf(countnum));
			}else if("ontime".equals(timetype)){
				form.setBuildwasteLetnumOntime(Integer.valueOf(countnum));
			}
		}else if("jzfqwczzpf_bg".equals(temlatecode)){
			if("overtime".equals(timetype)){
				form.setBuildwasteLetBgnumOvertime(Integer.valueOf(countnum));
			}else if("ontime".equals(timetype)){
				form.setBuildwasteLetBgnumOntime(Integer.valueOf(countnum));
			}
		}else if("jzfqwczzpf_yq".equals(temlatecode)){
			if("overtime".equals(timetype)){
				form.setBuildwasteLetYqnumOvertime(Integer.valueOf(countnum));
			}else if("ontime".equals(timetype)){
				form.setBuildwasteLetYqnumOntime(Integer.valueOf(countnum));
			}
		}else if("jzfqwczzxn_bg".equals(temlatecode)){
			if("overtime".equals(timetype)){
				form.setBuildwasteXnBgnumOvertime(Integer.valueOf(countnum));
			}else if("ontime".equals(timetype)){
				form.setBuildwasteXnBgnumOntime(Integer.valueOf(countnum));
			}
		}else if("jzfqwczzxn_yq".equals(temlatecode)){
			if("overtime".equals(timetype)){
				form.setBuildwasteXnyqnumOvertime(Integer.valueOf(countnum));
			}else if("ontime".equals(timetype)){
				form.setBuildwasteXnyqnumOntime(Integer.valueOf(countnum));
			}
		}
		
	}
	
	
	/**
	 * 导出文件
	 */
	public String exportStateExcel(Map map) {
		//{endtime=2012, starttime=2012, statetype=1, exporttype=excel, quarterselect=onequarter, yearselect=2010}
		String statetype=(String) map.get("statetype");
		String exporttype=(String) map.get("exporttype");
		String path="";
		if("excel".equals(exporttype)){
			path=this.writeExcel(map, null);
		}else if("word".equals(exporttype)){
			path=this.writeWord(map, null);
		}else if("pdf".equals(exporttype)){
			path=this.writePdf(map, null);
		}
		return path;
		
	}
	/**
	 * 根据输入的参数获取开始时间和结束时间
	 * @param map
	 * @return 
	 * @date 2012-4-21
	 */
	private String[] getTimeByParam(Map map){
		String statetype=(String) map.get("statetype");
		String starttime=(String) map.get("starttime");
		String endtime=(String) map.get("endtime");
		String quarterselect=(String) map.get("quarterselect");
		String yearselect=(String) map.get("yearselect");
		if("1".equals(statetype)){//年
			starttime=starttime+"-01-01";
			endtime=endtime+"-12-31";
		}else if("2".equals(statetype)){//季度
			if("1".equals(quarterselect)){
				starttime=yearselect+"-01-01";
				endtime=yearselect+"-03-31";
			}if("2".equals(quarterselect)){
				starttime=yearselect+"-04-01";
				endtime=yearselect+"-06-30";
			}if("3".equals(quarterselect)){
				starttime=yearselect+"-07-01";
				endtime=yearselect+"-09-30";
			}if("4".equals(quarterselect)){
				starttime=yearselect+"-10-01";
				endtime=yearselect+"-12-31";
			}
		}else if("3".equals(statetype)){//月
			starttime=starttime+"-01";
			String[] strs=endtime.split("-");
			  Calendar cal = Calendar.getInstance();      
		      cal.set(Calendar.YEAR, Integer.valueOf(strs[0])); 
		      
		      cal.set(Calendar.MONTH, Integer.valueOf(strs[1])-1);      
			  cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DATE));   
			  endtime=new  SimpleDateFormat( "yyyy-MM-dd ").format(cal.getTime());   

		}else{//为空的时候查询当前年
			starttime=DateUtilsEX.getCurYear()+"-01-01";
			endtime=DateUtilsEX.getCurYear()+"-12-31";
			
		}
		String[] times=new String[]{starttime,endtime};
		return times;
	}
	/**
	 * 返回路径String[0]觉对路径， String[1] app下的文件夹
	 * @return 
	 * @date 2012-4-21
	 */
	private String[] getStateAppPath(Map map,String subname){
		String contextpath=(String) map.get("contextpath");
		String realpathdir="/outfile/state";//webApp下文件夹
		String filename=GetFileName.getFileName(subname);
		String dir=contextpath+realpathdir;
		File dirFile=new File(dir);
		if(!dirFile.exists()){
			dirFile.mkdirs();
		}else{
			try {
				FileUtil.delDir(dir, filename);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		String path=dir+File.separator+filename;//文件路径
		path=path.replace("/",File.separator);
		String apppath=realpathdir+"/"+filename;
		return new String[]{path,apppath};
	}
	
	/**
	 * 写word
	 * @param map
	 * @param form 
	 * @date 2012-4-21
	 */
	private String writeWord(Map map,SummaryStateForm form){
		String[] paths=this.getStateAppPath(map,"doc");
		try {
			this.createWordDoc(paths[0], map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return paths[1];
	}
	/**
	 * 导入pdf
	 * @param map
	 * @param form
	 * @return 
	 * @date 2012-4-22
	 */
	private String writePdf(Map map,SummaryStateForm form){
		String[] paths=this.getStateAppPath(map,"pdf");
		try {
			this.createPdf(paths[0], map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return paths[1];
	}
	/**
	 * 查询审批业务的数量,用于判断excel合并的行
	 * @return 
	 * @date 2012-4-22
	 */
	private int getTempCountByType(String name){
		String sql="select count(1) as countnum from wf_bus_template t,wf_bus_template_type type \r";
		sql+="where type.id=t.template_type_id and type.template_type_name='"+name+"' and t.is_active=1";
		List list=this.ywXzXkSpStateDao.queryForList(sql);
		Map map=(Map) list.get(0);
		String countnum=(String) map.get("countnum");
		return Integer.valueOf(countnum);
	}
	/**
	 * 查询审批业务
	 * @return 
	 * @date 2012-4-22
	 */
	private List getTempCodeByType(String name){
		List names=new ArrayList();
		String sql="select t.template_name,t.template_code from wf_bus_template t,wf_bus_template_type type \r";
		sql+="where type.id=t.template_type_id and type.template_type_name='"+name+"' and t.is_active=1";
		List list=this.ywXzXkSpStateDao.queryForList(sql);
		for(int i=0;i<list.size();i++){
			Map map=(Map) list.get(i);
			String template_name=(String) map.get("template_name");
			template_name=this.changName(template_name);
			map.put("template_name",template_name);
			names.add(map);
		}
		return names;
	}
	
	/**
	 * 缩短业务名称
	 * @param name
	 * @return 
	 * @date 2012-4-22
	 */
	private String changName(String name){
		String tempname="";
		if(CommonUtils.containsStr(name,"管道")){
			tempname="燃气经营许可==>管道";
		}else if(CommonUtils.containsStr(name,"液化")){
			tempname="燃气经营许可==>液化气";
		}else if(CommonUtils.containsStr(name,"企业资质")){
			tempname="燃气企业企业资质";
		}else if(CommonUtils.containsStr(name,"设施改动")){
			tempname="燃气设施改动";
		}else if(CommonUtils.containsStr(name,"户外广告")){
			tempname="户外广告设置";
		}else if(CommonUtils.containsStr(name,"道路挖掘")){
			tempname="城市道路挖掘";
		}else if(CommonUtils.containsStr(name,"临时占用")){
			tempname="城市道路临时占用";
		}else if(CommonUtils.containsStr(name,"公厕")){
			tempname="拆除、关闭公厕许可";
		}else if(CommonUtils.containsStr(name,"水域") && CommonUtils.containsStr(name,"项目登记") ){
			tempname="水域城市生活垃圾服务许可==>项目登记";
		}else if(CommonUtils.containsStr(name,"水域") && !CommonUtils.containsStr(name,"项目登记")){
			tempname="水域城市生活垃圾服务许可";
		}else if(CommonUtils.containsStr(name,"陆域") && CommonUtils.containsStr(name,"项目登记") ){
			tempname="陆域城市生活垃圾服务许可==>项目登记";
		}else if(CommonUtils.containsStr(name,"陆域") && !CommonUtils.containsStr(name,"项目登记")){
			tempname="陆域城市生活垃圾服务许可";
		}else if(name.equals("从事城市生活垃圾经营性处置服务许可审批")){
			tempname="城市生活垃圾经营性处置服务许可";
		}else if(CommonUtils.containsStr(name,"建筑废弃物处置证") && CommonUtils.containsStr(name,"新增发证")){
			tempname="建筑废弃物处置证==>新增发证";
		}else if(CommonUtils.containsStr(name,"建筑废弃物处置证") && CommonUtils.containsStr(name,"年审续证")){
			tempname="建筑废弃物处置证==>年审续证";
		}else if(CommonUtils.containsStr(name,"建筑废弃物处置证") && CommonUtils.containsStr(name,"变更换证")){
			tempname="建筑废弃物处置证==>变更换证";
		}else if(CommonUtils.containsStr(name,"建筑废弃物处置证") && CommonUtils.containsStr(name,"损坏换证")){
			tempname="建筑废弃物处置证==>损坏换证";
		}else if(CommonUtils.containsStr(name,"建筑废弃物处置证") && CommonUtils.containsStr(name,"遗失补办")){
			tempname="建筑废弃物处置证==>遗失补办";
		}else{
			tempname=name;
		}
		return tempname;
	}
	
	
	/**
	 * 写excel文件
	 * @param map
	 * @param form
	 * @return 
	 * @date 2012-4-22
	 */
	private String writeExcel(Map map,SummaryStateForm form){
		String contextpath=(String) map.get("contextpath");
		 UserStateInfo info=(UserStateInfo) map.get("UserStateInfo");
		
		String inFilePath=contextpath+"/Templates/state/state.xls";
		if(info!=null){
			 String resultset=info.getStateitem();
			 if(resultset.split(",").length==1){
				 if(CommonUtils.containsStr(resultset, "完成时限")){
					 inFilePath=contextpath+"/Templates/state/statetimes.xls";
				 }
				 if(CommonUtils.containsStr(resultset, "审批结果")){
					 inFilePath=contextpath+"/Templates/state/stateresult.xls";
				 }
			 }
		 }
		inFilePath=inFilePath.replace("/", File.separator);
		String[] paths=this.getStateAppPath(map,"xls");
		writerDataToExcel(inFilePath, paths[0], form,map);
		return paths[1];
	}
	
	/**
	 * 显示的列
	 * @return 
	 * @date 2012-4-22
	 */
	private boolean isListcol(String name,String setname){
		 if(name.split(",").length==1){
			if(name.indexOf(setname)>0){
				return true;
			}else{
				return false;
			}
		 }
		return true;
	}
	 /**
     * 写入日期名称信息
     * @param inFilePath
     * @param outFilePath
     * @param entity
     * @return
     */
    private void writerDataToExcel(String inFilePath,String outFilePath,SummaryStateForm form,Map map){
    	
    	//WritableWorkbook wwb;
    	try {
    	  String item="审批结果,完成时限";//列
    	  String depts="燃气,景观,市容,设施,环卫,余泥";//行
    	  String buses="";
    	  UserStateInfo info=(UserStateInfo) map.get("UserStateInfo");
    	  if(info!=null){
    		 String stateBusiness=info.getStateBusiness();
    		 String[] bus=stateBusiness.split(",");
    		 for(String str:bus){
    			 if(str.indexOf("处")>0){
    				 str=str.substring(0,str.length()-1);
    			 }
    			 buses+=str+",";
    		 }
    		 if(buses.length()>0){
    			 buses=buses.substring(0,buses.length()-1);
    		 }
    		 depts=buses;
    		 item=info.getStateitem();
    	  }
    	
		   FileUtil.copyFile(inFilePath, outFilePath);
		   	//		 查询同意或者不同意
			List agreeData=ywXzXkSpStateDao.queryForList(getAgreeSql(map,null));
			//查询是否超时
			List overTimeData=ywXzXkSpStateDao.queryForList(getOverTimeSql(map,null));
		   String [] times=this.getTimeByParam(map);
		   String title="统计时间："+times[0]+"~"+times[1];
		   HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(outFilePath));
		   HSSFSheet sheet = workbook.getSheetAt(0);// 第一个工作表
		   HSSFCellStyle cs = workbook.createCellStyle();   
	       cs.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平居中   
		   cs.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//水平居中  
		   cs.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		   cs.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		   cs.setBorderRight(HSSFCellStyle.BORDER_THIN);
		   cs.setBorderTop(HSSFCellStyle.BORDER_THIN);
		   HSSFCellStyle cs1 = workbook.createCellStyle();   
	       cs1.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 水平居中   
		   cs1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//水平居中   
		   cs1.setBorderBottom(HSSFCellStyle.BORDER_THIN);
		   cs1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
		   cs1.setBorderRight(HSSFCellStyle.BORDER_THIN);
		   cs1.setBorderTop(HSSFCellStyle.BORDER_THIN);
		   String[] strs=depts.split(",");
			 int row=5;//定义总数
			 int ontimeCount=0;
	    	 int overtimeCount=0;
	    	 int agreeCount=0;
	    	 int noAgreeCount=0;
	    	 int gzCount=0;
	    	 int othercount=0;
		   for(String str:strs){
			   int row1=this.getTempCountByType(str)+1;//查询出一共有多少行，因为有个合计所以加1行
			   row1=row+row1-1;//excel 从0行开始算所以要-1
			   setCellData(sheet, cs, 1, 0, title);
			   // 四个参数分别是：起始行，起始列，结束行，结束列 
			   sheet.addMergedRegion(new   Region(row,(short)0,row1,(short)0)); 
		        setCellData(sheet, cs, row, 0, str+"处");   
		        List tempcodes=this.getTempCodeByType(str);
		    	int ontimeCountnum=0;
		    	int overtimeCountnum=0;
		    	int agreeCountnum=0;
		    	int noAgreeCountnum=0;
		    	int gzCountnum=0;
		    	int othercountnum=0;
		        for(int i=0;i<tempcodes.size();i++){
		        	Map tempmap=(Map) tempcodes.get(i);
		        	String tempanme=(String) tempmap.get("template_name");
		        	String tempcode=(String) tempmap.get("template_code");
		        	if(item.split(",").length==1){
		        		if(CommonUtils.containsStr(item, "完成时限")){//只有完成时限
		        			String ontimenum=getTimesData(overTimeData, tempcode,true);
		        			String overtimenum=getTimesData(overTimeData, tempcode,false);
		        			ontimeCountnum+=Integer.valueOf(ontimenum);
		        			overtimeCountnum+=Integer.valueOf(overtimenum);
		        			ontimeCount+=Integer.valueOf(ontimenum);
		        			overtimeCount+=Integer.valueOf(overtimenum);
		        			setCellData(sheet, cs1, row, 1, tempanme);
				        	setCellData(sheet, cs, row, 2, ontimenum);
				        	setCellData(sheet, cs, row, 3, "0");
				        	setCellData(sheet, cs, row, 4, overtimenum);
				        	row++;
				        	//加合计
				        	if(i==tempcodes.size()-1){
				        		setCellData(sheet, cs1, row, 1, "合计");
				        		setCellData(sheet, cs, row, 2, String.valueOf(ontimeCountnum));
				        		setCellData(sheet, cs, row, 3, String.valueOf(0));
				        		setCellData(sheet, cs, row, 4, String.valueOf(overtimeCountnum));
				        		row++;
				        	}
		        		}else{//只有审批结果
		        			String agreenum=getAgreeData(agreeData, tempcode, "zyxzxkjds",true);
		        			String noAgreenum=getAgreeData(agreeData, tempcode,"byxkjds",false);
		        			String othernum1=getAgreeData(agreeData, tempcode,"bysltzs",false);
		        			String othernum=getAgreeData(agreeData, tempcode,"",false);
		        			String gznum=getAgreeData(agreeData, tempcode,"bzcltzs",false);//一次性告知
		        			int samedata=this.getSameData(tempcode,true,map);//告知和准予同时都存在文书
		        			othernum=String.valueOf(Integer.valueOf(othernum1)+Integer.valueOf(othernum));
		        			othercountnum+=Integer.valueOf(othernum1);
		        		 	gznum=String.valueOf(Integer.valueOf(gznum)-samedata);
		        			agreeCountnum+=Integer.valueOf(agreenum);
		        			noAgreeCountnum+=Integer.valueOf(noAgreenum);
		        			agreeCount+=Integer.valueOf(agreenum);
					    	noAgreeCount+=Integer.valueOf(noAgreenum);
					    	gzCount+=Integer.valueOf(gznum);
					    	gzCountnum+=Integer.valueOf(gznum);
					    	othercount+=Integer.valueOf(othernum);
				        	setCellData(sheet, cs1, row, 1, tempanme);
				        	setCellData(sheet, cs, row, 2, agreenum);
				        	setCellData(sheet, cs, row, 3, noAgreenum);
				        	setCellData(sheet, cs, row, 4, gznum);
				        	setCellData(sheet, cs, row, 5, othernum);
				        	row++;
				        	//加合计
				        	if(i==tempcodes.size()-1){
				        		setCellData(sheet, cs1, row, 1, "合计");
				        		setCellData(sheet, cs, row, 2, String.valueOf(agreeCountnum));
				        		setCellData(sheet, cs, row, 3, String.valueOf(noAgreeCountnum));
				        		setCellData(sheet, cs, row, 4, String.valueOf(gzCountnum));
				        		setCellData(sheet, cs, row, 5, String.valueOf(othercountnum));
				        		row++;
				        	}
		        		}
		        	}else{//都显示
		        		String agreenum=getAgreeData(agreeData, tempcode, "zyxzxkjds",true);
	        			String noAgreenum=getAgreeData(agreeData, tempcode,"byxkjds",false);
	        			String othernum=getAgreeData(agreeData, tempcode,"",false);
	        			String othernum1=getAgreeData(agreeData, tempcode,"bysltzs",false);
	        			String gznum=getAgreeData(agreeData, tempcode,"bzcltzs",false);//一次性告知
		        		int samedata=this.getSameData(tempcode,true,map);//告知和准予同时都存在文书
	        			String ontimenum=getTimesData(overTimeData, tempcode,true);
	        			String overtimenum=getTimesData(overTimeData, tempcode,false);
	        			othercountnum+=Integer.valueOf(othernum);
	        			//其他类的包括撤案和不受理的
	        			othernum=String.valueOf(Integer.valueOf(othernum1)+Integer.valueOf(othernum));
	        			othercountnum+=Integer.valueOf(othernum1);
	        			agreeCountnum+=Integer.valueOf(agreenum);
	        	      	//一次性告知=一次性告知减去准予和一次性都存在的
				      	gznum=String.valueOf(Integer.valueOf(gznum)-samedata);
	        			noAgreeCountnum+=Integer.valueOf(noAgreenum);
	        			gzCountnum+=Integer.valueOf(gznum);
	        			ontimeCountnum+=Integer.valueOf(ontimenum);
	        			overtimeCountnum+=Integer.valueOf(overtimenum);
	        			agreeCount+=Integer.valueOf(agreenum);
				    	noAgreeCount+=Integer.valueOf(noAgreenum);
				    	gzCount+=Integer.valueOf(gznum);
				      	ontimeCount+=Integer.valueOf(ontimenum);
				      	othercount+=Integer.valueOf(othernum);
				      	overtimeCount+=Integer.valueOf(overtimenum);
				
			        	setCellData(sheet, cs1, row, 1, tempanme);
			        	setCellData(sheet, cs, row, 2, agreenum);
			        	setCellData(sheet, cs, row, 3, noAgreenum);
			        	setCellData(sheet, cs, row, 4, gznum);
			        	setCellData(sheet, cs, row, 5, othernum);
			        	setCellData(sheet, cs, row, 6, ontimenum);
			        	setCellData(sheet, cs, row, 7, "0");
			        	setCellData(sheet, cs, row, 8, overtimenum);
			        	row++;
			        	//加合计
			        	if(i==tempcodes.size()-1){
			        		setCellData(sheet, cs1, row, 1, "合计");
			        		setCellData(sheet, cs, row, 2, String.valueOf(agreeCountnum));
				        	setCellData(sheet, cs, row, 3, String.valueOf(noAgreeCountnum));
				        	setCellData(sheet, cs, row, 4, String.valueOf(gzCountnum));
				        	setCellData(sheet, cs, row, 5, String.valueOf(othercountnum));
				        	setCellData(sheet, cs, row, 6, String.valueOf(ontimeCountnum));
				        	setCellData(sheet, cs, row, 7, "0");
				        	setCellData(sheet, cs, row, 8, String.valueOf(overtimeCountnum));
			        		row++;
			        	}
		        	}
		        
		      
			    	
		        }
		        
		        if(item.split(",").length==1){
		        	if(CommonUtils.containsStr(item, "完成时限")){//只有完成时限
		        		setCellData(sheet, cs, 4, 2, String.valueOf(ontimeCount));
		        		setCellData(sheet, cs, 4, 3, String.valueOf(0));
		        		setCellData(sheet, cs, 4, 4, String.valueOf(overtimeCount));
		        }else {
		        	setCellData(sheet, cs, 4, 2, String.valueOf(agreeCount));
	        		setCellData(sheet, cs, 4, 3, String.valueOf(noAgreeCount));
	        		setCellData(sheet, cs, 4, 4, String.valueOf(gzCount));
	        		setCellData(sheet, cs, 4, 5, String.valueOf(othercount));
		        }
		       }else{//全委合计
		        	setCellData(sheet, cs, 4, 2, String.valueOf(agreeCount));
	        		setCellData(sheet, cs, 4, 3, String.valueOf(noAgreeCount));
	        		setCellData(sheet, cs, 4, 4, String.valueOf(gzCount));
	        		setCellData(sheet, cs, 4, 5, String.valueOf(othercount));
	        		setCellData(sheet, cs, 4, 6, String.valueOf(ontimeCount));
	        		setCellData(sheet, cs, 4, 7, String.valueOf(0));
	        		setCellData(sheet, cs, 4, 8, String.valueOf(overtimeCount));
		        }
		   }
	        FileOutputStream fos = new FileOutputStream(outFilePath);
			  
			  workbook.write(fos);
			  fos.close();   
		       
//		   Workbook rwb = Workbook.getWorkbook(new File(outFilePath));    
//			//打开一个文件的副本，并且指定数据写回到原文件         
//			 wwb = Workbook.createWorkbook(new File(outFilePath),rwb);//copy    
//			 WritableSheet ws = wwb.getSheet(0);
//			WritableFont font = new WritableFont(WritableFont.createFont("宋体"),
//					12);
//			WritableCellFormat wcf = new WritableCellFormat(font,
//					NumberFormats.TEXT);
//			wcf.setBorder(Border.ALL, BorderLineStyle.THIN);
//			wcf.setAlignment(Alignment.CENTRE);
//			wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
//			WritableFont font1 = new WritableFont(WritableFont.createFont("宋体"),
//					12,WritableFont.BOLD);
//			WritableCellFormat wcf1 = new WritableCellFormat(font,
//					NumberFormats.TEXT);
//			wcf1.setBorder(Border.ALL, BorderLineStyle.THIN);
//			wcf1.setAlignment(Alignment.LEFT);
//			wcf1.setVerticalAlignment(VerticalAlignment.CENTRE);
//			wcf1.setWrap(true);
//			String title="统计时间："+times[0]+"~"+times[1];
//			ExcelUtil.modiStrCell2(ws, 0, 1, title, wcf);// 标题
//			int row=5;
//			int row1=this.getTempCountByType("燃气")+1;//查询出一共有多少行，因为有个合计所以加1行
//			row1=row+row1-1;//excel 从0行开始算所以要-1
//			ExcelUtil.mergeCells(ws, 0, row, 0, row1);
//			ExcelUtil.modiStrCell2(ws, 0, 5, "燃气处", wcf1);// 燃气处
			
			
			// wwb.write();    
//	         wwb.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
    }  
    /**
     * 某个单元格写数据
     * @param hssfsheet
     * @param cs
     * @param rows
     * @param cols
     * @param content 
     * @date 2012-4-22
     */
	private void setCellData(HSSFSheet hssfsheet,HSSFCellStyle cs,int rows,int cols,String content){
		HSSFRow ssrow = hssfsheet.getRow((short) rows);
		ssrow.setHeight((short) 400);
        HSSFCell cell = ssrow.getCell((short) cols);
        if(cell!=null){
        	 cell.setCellStyle(cs);
             cell.setCellValue(content);
        }
       
	}
	
	/**
	 * 计算生成word的行和列
	 * @param map
	 * @return
	 */
	private int[] getRowAndCol(Map map){
	      String item="审批结果,完成时限";//列
	   	  String depts="燃气,景观,市容,设施,环卫,余泥";//行
		  String buses="";
		  UserStateInfo info=(UserStateInfo) map.get("UserStateInfo");
		  if(info!=null){
			 String stateBusiness=info.getStateBusiness();
			 String[] bus=stateBusiness.split(",");
			 for(String str:bus){
				 if(str.indexOf("处")>0){
					 str=str.substring(0,str.length()-1);
				 }
				 buses+=str+",";
			 }
			 if(buses.length()>0){
				 buses=buses.substring(0,buses.length()-1);
			 }
			 depts=buses;
			 item=info.getStateitem();
		   } 
		  String[] strs=depts.split(",");
		   int row=0;
		   for(String str:strs){
			    row+=this.getTempCountByType(str)+1;
		   }
		   //列
		   int col=0;
		   if(item.split(",").length==1){
			   if(item.indexOf("审批结果")>-1){
				   col=6; //因为要加前面3列，完成时限
			   }else{
				   col=5; //因为要加前面3列，再加同意不同意和一次性告知，和其他
			   }
		   }else{
			   col=9;
		   }
		   return new int[]{row+1,col};//有个汇总的列
	    	
	}
	
    /**
     * 生成word文件，生成word和pdf的方法太死没有excel生成方式灵活，导致重复代码太多，需要以后优化代码
     * @param path 
     * @throws IOException 
     * @throws DocumentException 
     * @date 2012-4-21
     */
    private void createWordDoc(String path,Map map) throws Exception{
    	SummaryStateForm summaryform=this.searchInfo(map);
    	int[] cols=this.getRowAndCol(map);//获取行和列
    	String context=(String) map.get("contextpath");
    	String[] times=this.getTimeByParam(map);
    	String img=context+"/gzcgwsp/state/rqstate/img/tongjiword.jpg";
    	img=img.replace("/",File.separator);
    	Document document=ItextWordUtil.ceateDocument(PageSize.A4);
    	ItextWordUtil.openDocument(document,path);
    	BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",   
                "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); 
    	ItextWordUtil.insertTitle(document,bfChinese,"广州城管委行政管理审批统计表", 12, Font.BOLD, Element.ALIGN_CENTER); 
    	
    	int width[] =new int[cols[1]]; 
    	if(cols[1]==5){
    		width[0]=15;width[1]=29;width[2]=16;width[3]=16;width[4]=16;
    	}else if(cols[1]==6){
    		width[0]=15;width[1]=29;width[2]=16;width[3]=16;width[4]=16;width[5]=16;
    	}else {
    		for(int k=0;k<9;k++){
    			if(k==0){
    				width[k]=20;
    			}else if(k==1){
    				width[k]=28;
    			}else if(k==4){
    				width[k]=18;
    			}else{
    				width[k]=12;
    			}
    		}
    		
    	}
    	 //因为itext 创建列不能自动调整高度，所以用两行来表示一行加高
    	Table table=ItextWordUtil.creatTeable(cols[0]*2,cols[1],width,new Color(0, 125, 255));
    	Image photo=null;
  		try {
  			photo = Image.getInstance(img);
  			photo.scaleAbsolute(180, 50);
  		} catch (Exception e) {
  			e.printStackTrace();
  		} 
    	Font fontChinese = new Font(bfChinese, 10, Font.BOLD); 
    	String statetime="统计时段:"+times[0]+"~"+times[1];
    	ItextWordUtil.createCell(table,statetime,fontChinese,cols[1],2,new Color(0, 0, 0),Element.ALIGN_CENTER,Element.ALIGN_CENTER);
    	ItextWordUtil.createCellByImage(table,photo,fontChinese,2,4,new Color(0, 0, 0));
    	Map<String,Integer> countMap=getCount(map);
    	Map dataMap=findData(map);
    	setTable(table, fontChinese, map, dataMap,countMap,cols);
    	//setTable(table, fontChinese,map,summaryform);
    	document.add(table);   
        document.add(new Paragraph("\n"));   
        ItextWordUtil.closeDocument(document);
    	
    }
    
    /**
     * 写入数据
     * @param table
     * @param fontChinese
     * @param map
     * @param dataMap 每项业务数据
     * @param countMap 汇总数据
     */
    private void setTable(Table table, Font fontChinese, Map map, Map dataMap,
			Map<String, Integer> countMap,int[]cols) {
       String item="审批结果,完成时限";//列
  	   String depts="燃气,景观,市容,设施,环卫,余泥";//行
  	   String buses="";
  	   UserStateInfo info=(UserStateInfo) map.get("UserStateInfo");
  	   if(info!=null){
  		 String stateBusiness=info.getStateBusiness();
  		 String[] bus=stateBusiness.split(",");
  		 for(String str:bus){
  			 if(str.indexOf("处")>0){
  				 str=str.substring(0,str.length()-1);
  			 }
  			 buses+=str+",";
  		 }
  		 if(buses.length()>0){
  			 buses=buses.substring(0,buses.length()-1);
  		 }
  		 depts=buses;
  		 item=info.getStateitem();
  	   }
      	if(item.indexOf("审批结果")>-1){
      		ItextWordUtil.createCell(table,"审批结果",fontChinese,4,2,new Color(0, 0, 0),Element.ALIGN_CENTER,Element.ALIGN_CENTER);
        	}
      	if(item.indexOf("完成时限")>-1){
      	  ItextWordUtil.createCell(table,"完成时限",fontChinese,3,2,new Color(0, 0, 0),Element.ALIGN_CENTER,Element.ALIGN_CENTER);
      	}
      	if(item.indexOf("审批结果")>-1){
  	    	ItextWordUtil.createCell(table,"准予许可",fontChinese,1,2,new Color(0, 0, 0),Element.ALIGN_CENTER,Element.ALIGN_CENTER);
  	    	ItextWordUtil.createCell(table,"不予许可",fontChinese,1,2,new Color(0, 0, 0),Element.ALIGN_CENTER,Element.ALIGN_CENTER);
  	    	ItextWordUtil.createCell(table,"一次性告知",fontChinese,1,2,new Color(0, 0, 0),Element.ALIGN_CENTER,Element.ALIGN_CENTER);
  	    	ItextWordUtil.createCell(table,"其他",fontChinese,1,2,new Color(0, 0, 0),Element.ALIGN_CENTER,Element.ALIGN_CENTER);

      	}
      	if(item.indexOf("完成时限")>-1){
  	    	ItextWordUtil.createCell(table,"按期完成(宗)",fontChinese,1,2,new Color(0, 0, 0),Element.ALIGN_CENTER,Element.ALIGN_CENTER);
  	    	ItextWordUtil.createCell(table,"延期完成(宗)",fontChinese,1,2,new Color(0, 0, 0),Element.ALIGN_CENTER,Element.ALIGN_CENTER);
  	    	ItextWordUtil.createCell(table,"超期完成(宗)",fontChinese,1,2,new Color(0, 0, 0),Element.ALIGN_CENTER,Element.ALIGN_CENTER);
      	}	
      	ItextWordUtil.createCell(table,"受理(宗)",fontChinese,2,2,new Color(0, 0, 0),Element.ALIGN_CENTER,Element.ALIGN_CENTER);
      	//汇总
      	if(item.indexOf("审批结果")>-1){
  	    	ItextWordUtil.createCell(table,countMap.get("agreecount")+"",fontChinese,1,2,new Color(0, 0, 0),Element.ALIGN_CENTER,Element.ALIGN_CENTER);
  	    	ItextWordUtil.createCell(table,countMap.get("notagreecount")+"",fontChinese,1,2,new Color(0, 0, 0),Element.ALIGN_CENTER,Element.ALIGN_CENTER);
  	    	ItextWordUtil.createCell(table,countMap.get("gzcount")+"",fontChinese,1,2,new Color(0, 0, 0),Element.ALIGN_CENTER,Element.ALIGN_CENTER);
  	    	ItextWordUtil.createCell(table,countMap.get("othercount")+"",fontChinese,1,2,new Color(0, 0, 0),Element.ALIGN_CENTER,Element.ALIGN_CENTER);

      	}
      	if(item.indexOf("完成时限")>-1){
  	    	ItextWordUtil.createCell(table,countMap.get("ontimecount")+"",fontChinese,1,2,new Color(0, 0, 0),Element.ALIGN_CENTER,Element.ALIGN_CENTER);
  	    	ItextWordUtil.createCell(table,(countMap.get("yqcount")==null?0:countMap.get("yqcount"))+"",fontChinese,1,2,new Color(0, 0, 0),Element.ALIGN_CENTER,Element.ALIGN_CENTER);
  	    	ItextWordUtil.createCell(table,countMap.get("overtimecount")+"",fontChinese,1,2,new Color(0, 0, 0),Element.ALIGN_CENTER,Element.ALIGN_CENTER);
      	}
      	Set set=dataMap.keySet();
  		for (Iterator it = set.iterator(); it.hasNext();) {
  			String  name =  (String) it.next();
  			Map data=(Map) dataMap.get(name);
  			int col=getTempCountByType(name)+1;//有一个合计所以加1
 	    	ItextWordUtil.createCell(table,name,fontChinese,1,col*2,new Color(0, 0, 0),Element.ALIGN_CENTER,Element.ALIGN_CENTER);    	
 	    	Set tempset=data.keySet();
 	    	for (Iterator iter = tempset.iterator(); iter.hasNext();){
	    		String  tempname =  (String) iter.next();
 	    		int[] value=(int[]) data.get(tempname);
 	    		ItextWordUtil.createCell(table,tempname,fontChinese,1,2,new Color(0, 0, 0),Element.ALIGN_LEFT,Element.ALIGN_LEFT);
	  	    	if(item.indexOf("审批结果")>-1){
	  		    	ItextWordUtil.createCell(table,String.valueOf(value[0]),fontChinese,1,2,new Color(0, 0, 0),Element.ALIGN_CENTER,Element.ALIGN_CENTER);
	  		    	ItextWordUtil.createCell(table,""+value[1],fontChinese,1,2,new Color(0, 0, 0),Element.ALIGN_CENTER,Element.ALIGN_CENTER);
	  		    	ItextWordUtil.createCell(table,""+value[2],fontChinese,1,2,new Color(0, 0, 0),Element.ALIGN_CENTER,Element.ALIGN_CENTER);
	  		    	ItextWordUtil.createCell(table,""+value[3],fontChinese,1,2,new Color(0, 0, 0),Element.ALIGN_CENTER,Element.ALIGN_CENTER);
	  	    	}
	  	    	if(item.indexOf("完成时限")>-1){
	  		    	ItextWordUtil.createCell(table,""+value[4],fontChinese,1,2,new Color(0, 0, 0),Element.ALIGN_CENTER,Element.ALIGN_CENTER);
	  		    	ItextWordUtil.createCell(table,""+value[5],fontChinese,1,2,new Color(0, 0, 0),Element.ALIGN_CENTER,Element.ALIGN_CENTER);
	  		    	ItextWordUtil.createCell(table,""+value[6],fontChinese,1,2,new Color(0, 0, 0),Element.ALIGN_CENTER,Element.ALIGN_CENTER);
	  	    	
	  	    	}
 	    	}

  		 }
	}
	/**
     * 生成pdf文件
     * @param path 
     * @throws IOException 
     * @throws DocumentException 
     * @date 2012-4-21
     */
    private void createPdf(String path,Map map) throws Exception{
    	SummaryStateForm form=searchInfo(map);
    	int[] cols=this.getRowAndCol(map);//获取行和列
    	String context=(String) map.get("contextpath");
    	String[] times=this.getTimeByParam(map);
    	String img=context+"/gzcgwsp/state/rqstate/img/pdf2.jpg";
    	img=img.replace("/",File.separator);
    	 //左、右、上、下页边距   
	       Document doc =new Document(PageSize.A4, 50, 50, 10, 10);    
	      //A4横向    
		//  Document doc = new Document(PageSize.A4.rotate());   
	     PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(path));   
	     BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",   
	             "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);  
	     Font titleFont = new com.lowagie.text.Font(bfChinese, 18, com.lowagie.text.Font.NORMAL);   
			   //添加footer需要在document.open之前   
	     doc.open();   
	     Paragraph title = new Paragraph("广州城管委行政管理审批统计表 ",titleFont);   
		 title.setAlignment(Paragraph.ALIGN_CENTER);           
		 doc.add(title); 
		 int width[] =new int[cols[1]]; 
		   	if(cols[1]==5){
	    		width[0]=20;width[1]=25;width[2]=16;width[3]=16;width[4]=16;
	    	}if(cols[1]==6){
	    		width[0]=20;width[1]=25;width[2]=16;width[3]=16;width[4]=16;width[5]=16;
	    	}else {
	    		for(int k=0;k<9;k++){
	    			if(k==0){
	    				width[k]=20;
	    			}else if(k==1){
	    				width[k]=25;
	    			}else if(k==2||k==3){
	    				width[k]=12;
	    			}else if(k==4){
	    				width[k]=18;
	    			}else{
	    				width[k]=12;
	    			}
	    		}
	    	}
	    	 //因为itext 创建列不能自动调整高度，所以用两行来表示一行加高
	     Table table=ItextWordUtil.creatTeable(cols[0]*2,cols[1],width,new Color(0, 125, 255));
         table.setWidths(width);// 设置每列所占比例   
         table.setWidth(100); // 占页面宽度 90% 
         table.setAlignment(Element.ALIGN_CENTER);// 居中显示   
         table.setAlignment(Element.ALIGN_MIDDLE);// 纵向居中显示   
         table.setAutoFillEmptyCells(true); // 自动填满   
         table.setBorderWidth(0); // 边框宽度   
         table.setBorderColor(new Color(0, 125, 255)); // 边框颜色      
        Font fontChinese = new Font(bfChinese, 10, Font.BOLD);         
    	Image photo=null;
  		try {
  			photo = Image.getInstance(img);
  			photo.scalePercent(50, 43);
  		} catch (Exception e) {
  			e.printStackTrace();
  		}  
    	String statetime="统计时段:"+times[0]+"~"+times[1];
    	ItextWordUtil.createCell(table,statetime,fontChinese,cols[1],2,new Color(0, 0, 0),Element.ALIGN_CENTER,Element.ALIGN_CENTER);
    	ItextWordUtil.createCellByImage(table,photo,fontChinese,2,4,new Color(0, 0, 0));
    	Map<String,Integer> countMap=getCount(map);
    	Map dataMap=findData(map);
    	setTable(table, fontChinese, map, dataMap,countMap,cols);
    	
    	doc.add(table);   
        ItextWordUtil.closeDocument(doc);
        writer.close();
    }
    

    
    /**
     * 查询用户的自定义统计设置
     */
	public UserStateInfo getUserStateInfo(Long userid) {
		String hql=" from UserStateInfo where userId="+userid;
		List list=this.ywXzXkSpStateDao.find(hql);
		if(list.size()>0){
			return (UserStateInfo) list.get(0);
		}
		return null;
		
	}
	/**
	 * 保存自定义设置
	 */
	public void saveUserStateInfo(UserStateInfo entity) {
	   Session session=this.ywXzXkSpStateDao.getSession();
	   session.saveOrUpdate(entity);
		
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.augurit.gzcgwsp.state.service.ISummaryService#findData(java.util.Map)
	 */
	@Override
	public Map findData(Map map) {
		Map<String,Map> maps=new LinkedHashMap<String,Map>();
		List<Map> list=new ArrayList<Map>();
		UserStateInfo info=(UserStateInfo) map.get("UserStateInfo"); 
		if(info!=null){
			String depts=info.getStateBusiness();
			String[] strs=depts.split(",");
			Map<String,Integer> dataMap=new LinkedHashMap<String,Integer>();
			  for(String str:strs){
				  	String codeSql="select temp.template_code,temp.template_name from wf_bus_template temp ," +
				  			"wf_bus_template_type ty where  temp.template_type_id=ty.id   and temp.is_active='1' " +
				  			" and ty.template_type_name='"+str+"' order by temp.id asc"; 
				    List codes=this.ywXzXkSpStateDao.queryForList(codeSql);
				    Map<String,String> countName=new HashMap<String,String>();
				    countName.put("template_name", "合计");//加一栏合计
				    codes.add(countName);
				  	String agreeSql=this.getAgreeSql(map,"'"+str+"'");
					String overTimeSql=this.getOverTimeSql(map,str);
					//查询同意或者不同意
					List agreeData=this.ywXzXkSpStateDao.queryForList(agreeSql);
					//查询是否超时
					List overTimeData=this.ywXzXkSpStateDao.queryForList(overTimeSql);
					int[] counts=new int[7];
					for(int i=0;i<overTimeData.size();i++){
						Map data=(Map) overTimeData.get(i);
						String timetype=(String) data.get("timetype");
						String template_name=(String)data.get("template_name");
						String countnum=(String) data.get("countnum")==null?"0":(String) data.get("countnum");
						String ontimekey=template_name+"_ontime";
						String overtimekey=template_name+"_overtime";
						if("ontime".equals(timetype)){
							dataMap.put(ontimekey,Integer.valueOf(countnum));
							Integer ontimeallcount=(Integer) dataMap.get("ontimeallcount")==null?0:(Integer) dataMap.get("ontimeallcount");
							ontimeallcount+=Integer.valueOf(countnum);
							counts[4]=ontimeallcount;
							dataMap.put("ontimeallcount",ontimeallcount);
						}else if("overtime".equals(timetype)){
							dataMap.put(overtimekey,Integer.valueOf(countnum));
							counts[6]+=Integer.valueOf(countnum);
							dataMap.put("overtimeallcount",Integer.valueOf(countnum));
						}
					}
					
					for(int i=0;i<agreeData.size();i++){
						Map data=(Map) agreeData.get(i);
						String template_name=(String)data.get("template_name");
						String template_type_name=(String)data.get("template_type_name");
						String filecode=(String) data.get("file_code");
						String tempcode=(String) data.get("template_code");
						String countnum=(String) data.get("countnum")==null?"0":(String) data.get("countnum");
						String agreekey=template_name+"_agree";
						String gzkey=template_name+"_gz";
						String noAgreekey=template_name+"_noAgree";
						String otherkey=template_name+"_other";
						if(!"hwggshsqb".equals(tempcode)){//户外广告除外
							if(StringUtils.isNotBlank(filecode)){
								if("zyxzxkjds".equals(filecode)||"quzyxzxkjds".equals(filecode)){
									int agree=dataMap.get(agreekey)==null?0:dataMap.get(agreekey);
									int num=agree+Integer.valueOf(countnum);
									dataMap.put(agreekey,num);
									counts[0]+=Integer.valueOf(countnum);
								}else if("byxkjds".equals(filecode)||"qubyxkjds".equals(filecode)){
									int noagree=dataMap.get(noAgreekey)==null?0:dataMap.get(noAgreekey);
									int num=noagree+Integer.valueOf(countnum);
									dataMap.put(noAgreekey,num);
									counts[1]+=Integer.valueOf(countnum);
								}else if("bzcltzs".equals(filecode)||"qubzcltzs".equals(filecode)){
									int sameData=this.getSameData(tempcode,true,map);
									int count=Integer.valueOf(countnum)-sameData;
									int gz=dataMap.get(gzkey)==null?0:dataMap.get(gzkey);
									int gzcount=gz+count;
									counts[2]+=Integer.valueOf(gzcount);
									dataMap.put(gzkey,gzcount);
								}else if("bysltzs".equals(filecode)||"qubysltzs".equals(filecode)){//不予受理的案件属于其他
									int other=dataMap.get(otherkey)==null?0:dataMap.get(otherkey);;
									int othercount=other+Integer.valueOf(countnum);
									int countother=dataMap.get("othercount")==null?0:dataMap.get("othercount");;
									counts[3]=countother+Integer.valueOf(countnum);
									dataMap.put("othercount",counts[3]);
									dataMap.put(otherkey,othercount);
								}
							}else{
								if("余泥".equals(template_type_name)){
									//没文书看成通过
									int agree=dataMap.get(agreekey)==null?0:dataMap.get(agreekey);
									int num=agree+Integer.valueOf(countnum);
									dataMap.put(agreekey,num);
									counts[0]+=Integer.valueOf(countnum);
								}else{
									//没有文书的案件属于其他
									int other=dataMap.get(otherkey)==null?0:dataMap.get(otherkey);
									int countother=dataMap.get("othercount")==null?0:dataMap.get("othercount");;
									counts[3]=countother+Integer.valueOf(countnum);
									int othercount=other+Integer.valueOf(countnum);
									dataMap.put(otherkey,othercount);
									dataMap.put("othercount",counts[3]);
								}
							
							}
					}else{
							if("byxkjds".equals(filecode)||"qubyxkjds".equals(filecode)){
								dataMap.put(noAgreekey,Integer.valueOf(countnum));
								counts[1]+=Integer.valueOf(countnum);
							}else{
								dataMap.put(agreekey,Integer.valueOf(countnum));
								counts[0]+=Integer.valueOf(countnum);
							}
					}
					list.add(dataMap);
				 }
				Map<String,int[]> tempMap=new LinkedHashMap<String,int[]>();	
				Map<String,int[]> countMap=new LinkedHashMap<String,int[]>();	
				List<Map> templist=new ArrayList<Map>();
				
				for(int k=0;k<codes.size();k++){
					Map temp=(Map) codes.get(k);
					String tempName=(String) temp.get("template_name");
					int[] tempvals=new int[7];
					for(Map data:list){
						Iterator it=data.entrySet().iterator();
			            while(it.hasNext()){
			         	   Map.Entry entrys=(Map.Entry)it.next();
			         	   String key=(String) entrys.getKey();
			         	   if((tempName+"_agree").equals(key)){
			         		  int count=(Integer) data.get(tempName+"_agree");
			         		  tempvals[0]=count;
			         	   }else if((tempName+"_noAgree").equals(key)){
			         		  int count=(Integer) data.get(tempName+"_noAgree");
			         		  tempvals[1]=count;
			         	   }else if((tempName+"_gz").equals(key)){
			         		  int count=(Integer) data.get(tempName+"_gz");
			         		  tempvals[2]=count;
				         	}else if((tempName+"_other").equals(key)){
			         		  int count=(Integer) data.get(tempName+"_other");
			         		  tempvals[3]=count;
				         	}else if((tempName+"_ontime").equals(key)){
			         		   int count=(Integer) data.get(tempName+"_ontime");
			         		   tempvals[4]=count;
					        }else if((tempName+"_overtime").equals(key)){
					        	int count=(Integer) data.get(tempName+"_overtime");
				         		 tempvals[5]=count;
					        }
			          }
				}
				if("合计".equals(tempName)){
					 tempvals=counts;
					 tempMap.put(tempName, tempvals);
					 dataMap.put("othercount",0);
					 dataMap.put("ontimeallcount",0);//避免下一个业务重复
				}
				  tempMap.put(tempName, tempvals);
				  templist.add(tempMap);
			}
			 //countMap.put(str.trim()+"count", counts);	
			 maps.put(str.trim(), tempMap);

			 //每种业务的合计
			}	
		 }
		return maps;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.augurit.gzcgwsp.state.service.ISummaryService#getCount(java.util.Map)
	 */
	@Override
	public Map<String, Integer> getCount(Map map) {
		Map<String,Integer> countMap=new HashMap<String,Integer>();
		String agreeSql=this.getAgreeSql(map,null);
		String overTimeSql=this.getOverTimeSql(map,null);
		//查询同意或者不同意
		List agreeData=this.ywXzXkSpStateDao.queryForList(agreeSql);
		//查询是否超时
		List overTimeData=this.ywXzXkSpStateDao.queryForList(overTimeSql);
		int agreecount=0;
		int notagreecount=0;
		int ontimecount=0;
		int overtimecount=0;
		int gzcount=0;
		int othercount=0;
		for(int i=0;i<agreeData.size();i++){
				Map data=(Map) agreeData.get(i);
				String filecode=(String) data.get("file_code");
				String tempcode=(String) data.get("template_code");
				String countnum=(String) data.get("countnum")==null?"0":(String) data.get("countnum");
			  if(!"hwggshsqb".equals(tempcode)){//户外广告除外
				if(StringUtils.isNotBlank(filecode)){
					if("zyxzxkjds".equals(filecode)||"quzyxzxkjds".equals(filecode)){
						agreecount+=Integer.valueOf(countnum);
					}else if("byxkjds".equals(filecode)||"qubyxkjds".equals(filecode)){
						notagreecount+=Integer.valueOf(countnum);
					}else if("bzcltzs".equals(filecode)||"qubzcltzs".equals(filecode)){
						gzcount+=Integer.valueOf(countnum);
					}else if("bysltzs".equals(filecode)||"qubysltzs".equals(filecode)){
						othercount+=Integer.valueOf(countnum);
					}
				}else{
					WfBusTemplateForm template=wfBusTemplateService.getByTemplateCode(tempcode);
			    	Long tempTypeId=template.getTemplateTypeId();
			    	WfBusTemplateTypeForm type=wfBusTemplateTypeService.get(tempTypeId);	
					if("hwggshsqb".equals(tempcode) ||"余泥".equals(type.getTemplateTypeName())
							){
					   //如果有些业务没有文书就当成通过
						agreecount+=Integer.valueOf(countnum);
					}else if("环卫".equals(type.getTemplateTypeName())){
						agreecount+=Integer.valueOf(countnum);
					}else{
						othercount+=Integer.valueOf(countnum);
					}
				}
			}else{
					//户外广告设置没有制文 和其他的统计不同
					if("byxkjds".equals(filecode)){//不同意
						notagreecount+=Integer.valueOf(countnum);
					}else{//没有文书表示同意
						agreecount+=Integer.valueOf(countnum);
					}
			}
		}
		for(int i=0;i<overTimeData.size();i++){
			Map data=(Map) overTimeData.get(i);
			String timetype=(String) data.get("timetype");
			String countnum=(String) data.get("countnum")==null?"0":(String) data.get("countnum");
			if("overtime".equals(timetype)){
				overtimecount+=Integer.valueOf(countnum);
			}else if("ontime".equals(timetype)){
				ontimecount+=Integer.valueOf(countnum);
			}
		}
		int samedatacount=this.getSameData("", false,map);
		if(gzcount>=samedatacount){
			gzcount=gzcount-samedatacount;
		}
		countMap.put("agreecount", agreecount);
		countMap.put("notagreecount", notagreecount);
		countMap.put("overtimecount", overtimecount);
		countMap.put("gzcount", gzcount);
		countMap.put("othercount", othercount);
		countMap.put("ontimecount", ontimecount);
		countMap.put("othercount", othercount);//其他
		return countMap;
	}
	/*
	 * (non-Javadoc)
	 * @see com.augurit.gzcgwsp.state.service.ISummaryService#saveUserStateByRole(java.lang.Long[])
	 */
	@Override
	public void saveUserStateByRole(Long[] roleids) {
	  
		
	}
	/**
	 * 各区的统计
	 */
	@Override
	public Map findAreaData(Map map) {
		List<BaseDistrict> list=ywSummaryStateService.getArea();
		String sql=getAreaSql(map);
	    List data=this.ywXzXkSpStateDao.queryForList(sql);
	    Map<String,int[]> areaMap=new LinkedHashMap<String,int[]>();
	    if(data!=null &&data.size()>0){
		    	for(BaseDistrict entity:list){
		    		int[] num=new int[6];
		    		String areaname=entity.getDistrictName();
		    	    for(int i=0;i<data.size();i++){
				    	Map datamap=(Map) data.get(i);
				    	String area=(String) datamap.get("bus_memo2")==null?"市城管委":(String) datamap.get("bus_memo2");
				    	int countnum=(String) datamap.get("countnum")==null?0:Integer.valueOf((String) datamap.get("countnum"));
				    	String type_name=(String) datamap.get("template_type_name");
					   if("燃气".equals(type_name) && areaname.equals(area)){
							num[0]=countnum;
				    	}else if("户外广告".equals(type_name) && areaname.equals(area)){
				    		num[1]=countnum;
				    	}else if("设施".equals(type_name) && areaname.equals(area)){
				    		num[2]=countnum;
				    	}else if("环卫".equals(type_name) && areaname.equals(area)){
				    		num[3]=countnum;
				    	}else if("余泥".equals(type_name) && areaname.equals(area)){
				    		num[4]=countnum;
				    	}else if("市容".equals(type_name) && areaname.equals(area)){
				    		num[5]=countnum;
				    	}
		    	 }
		    	  areaMap.put(areaname, num);
	      }
	    }
		
		
		return areaMap;
	}
	/**
	 * 构建sql,凡是有准予许可决定书的案件就为同意
	 * @param map
	 * @return 
	 * @date 2012-4-19
	 */
	private String getAreaSql(Map map){
		String []times=this.getTimeByParam(map);
		StringBuffer sb=new StringBuffer();
		sb.append("select  count(1) as countnum,ins.bus_memo2,temptype.template_type_name from   \r");
		sb.append(" JBPM4_HIST_PROCINST proc,WF_BUS_INSTANCE ins,  \r");
		sb.append(" WF_BUS_TEMPLATE temp,wf_bus_template_type temptype  where proc.id_=ins.proc_inst_id  \r");  
		sb.append(" and temptype.id=temp.template_type_id   \r");
		sb.append(" and ins.template_id=temp.id    and proc.state_='ended'  \r");
		sb.append(" and ins.bus_date2>=to_date('"+times[0]+"','yyyy-MM-dd') \r");
		sb.append(" and ins.bus_date2<=to_date('"+times[1]+"','yyyy-MM-dd') \r");
		sb.append(" group by ins.bus_memo2,temptype.template_type_name \r");
		return sb.toString();
	}
	
	
	@Override
	public List getAreacountData(Map map) {
		String []times=this.getTimeByParam(map);
		StringBuffer sb=new StringBuffer();
		sb.append("select  count(1) as countnum,temptype.template_type_name from   \r");
		sb.append(" JBPM4_HIST_PROCINST proc,WF_BUS_INSTANCE ins,  \r");
		sb.append(" WF_BUS_TEMPLATE temp,wf_bus_template_type temptype  where proc.id_=ins.proc_inst_id  \r");  
		sb.append(" and temptype.id=temp.template_type_id   \r");
		sb.append(" and ins.template_id=temp.id    and proc.state_='ended'  \r");
		sb.append(" and ins.bus_date2>=to_date('"+times[0]+"','yyyy-MM-dd') \r");
		sb.append(" and ins.bus_date2<=to_date('"+times[1]+"','yyyy-MM-dd') \r");
		sb.append(" group by temptype.template_type_name \r");
		List data=this.ywXzXkSpStateDao.queryForList(sb.toString());
		return data;
	}
}
